class MyLinkedList {

    static class ListNode{
        int val;
        ListNode next;
        public ListNode(int val){
            this.val = val;
        }
        public ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }
    public ListNode head;
    public int Size;

    public MyLinkedList() {
        this.head = null;
    }

    public int get(int index) {
        if(index<0||index>=Size){
            return -1;
        }else{
            ListNode cur = this.head;
            while(index>0){
                cur = cur.next;
                index--;
            }
            return cur.val;
        }
    }

    public void addAtHead(int val) {
        ListNode cur = new ListNode(val);
        cur.next = head;
        this.head = cur;
        this.Size++;
    }

    public void addAtTail(int val) {
        if(this.head == null){
            this.head = new ListNode(val);
            this.Size++;
            return ;
        }
        ListNode cur = head;
        while(cur.next!=null){
            cur = cur.next;
        }
        ListNode newnode = new ListNode(val);
        cur.next = newnode;
        this.Size++;
    }

    public void addAtIndex(int index, int val) {
        if(index<=0){
            addAtHead(val);
            return ;
        }else if(index>this.Size){
            return ;
        }else if(index==this.Size){
            addAtTail(val);
            return ;
        }
        else{
            ListNode cur = this.head;
            while(index-1>0&&cur.next!=null){
                index--;
                cur = cur.next;
            }
            ListNode newnode = new ListNode(val);
            newnode.next = cur.next;
            cur.next = newnode;
            this.Size++;
        }

    }

    public void deleteAtIndex(int index) {
        if(index<0||index>=Size){
            return ;
        }else if(index==0){
            this.head = this.head.next;
            this.Size--;
            return ;
        }else{
            ListNode cur = this.head;
            while(cur.next!=null&&index-1>0){
                cur = cur.next;
                index--;
            }
            cur.next = cur.next.next;
            this.Size--;
            return ;
        }
    }
    public void display(){
        ListNode cur = this.head;

        while(cur!=null){
            System.out.print(cur.val+"->");
            cur = cur.next;
        }
        System.out.println("null");
    }
    public ListNode deleteDuplicates(ListNode head) {
        if(head==null){
            return null;
        }

        ListNode dummy = new ListNode(-1,head);
        ListNode prev = dummy;
        ListNode cur = prev.next;
        ListNode next = null;
        boolean again = false;
        while(cur!=null&&cur.next!=null){
            cur = prev.next;
            next = cur.next;
            again = false;
            while(next!=null&&next.val==cur.val){
                again = true;
                next = next.next;
            }
            if(again){
                prev.next = next;
            }else{
                prev = prev.next;
            }
        }
        return dummy.next;

    }
}
public class TextDemo {
    public static void main(String[] args) {
        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.addAtTail(1);
        myLinkedList.addAtTail(2);
        myLinkedList.addAtTail(3);
        myLinkedList.addAtTail(3);
        myLinkedList.addAtTail(4);
        myLinkedList.addAtTail(4);
        myLinkedList.addAtTail(5);
        myLinkedList.display();
        myLinkedList.deleteDuplicates(myLinkedList.head);
        myLinkedList.display();

    }

}
